package day0309

import (
	"fmt"
	. "gocode/algorithm/utils"
)

var ans []*TreeNode
var count map[string]int

func findDuplicateSubtrees(root *TreeNode) []*TreeNode {
	ans = make([]*TreeNode, 0)
	count = make(map[string]int)
	help_(root)
	return ans
}

func help_(root *TreeNode) string {
	if root == nil {
		return "#"
	}
	str := fmt.Sprintf("%d,%s,%s", root.Val, help_(root.Left), help_(root.Right))
	count[str]++
	if count[str] == 2 {
		ans = append(ans, root)
	}
	return str
}
